package com.qf.cardlikeboot.service.impl;

import com.qf.cardlikeboot.domain.User;
import com.qf.cardlikeboot.mapper.UserMapper;
import com.qf.cardlikeboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    @Transactional(readOnly = true, propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    public User login(String name, String password) {
        List<User> users = userMapper.selectByName(name);
        // 循环判断用户的密码是否跟传递的密码一致
        for (User user : users) {
            if (Objects.equals(password, user.getPassword())) {
                return user;
            }
        }
        return null;
    }

    @Override
    public List<User> selectByNameFuzzy(String name) {
        return userMapper.selectByNameFuzzy(name);
    }
}
